package com.qdu.controller;

import com.qdu.entity.Houseinfo;
import com.qdu.entity.Shareinformation;
import com.qdu.service.HouseService;
import com.qdu.service.ShareInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/shareinformations")
public class ShareinformationController {

    @Autowired
    private ShareInformationService shareInformationService;

    @Autowired
    HouseService houseService;

    @PostMapping("/add")
    public Result insert(@RequestBody Shareinformation record){
        System.out.println(record);
        Result result = new Result();
        Shareinformation shareinformation = shareInformationService.selectByHid(record.getHid());
        System.out.println("shareinfomation"+shareinformation);
        if(shareinformation == null){
            shareInformationService.insert(record);
            result.setMsg("参加合租成功");
        }
        else{
            if(shareinformation.getMid().equals(record.getMid())){
                if(shareinformation.getHid().equals(record.getHid())){
                    if(shareinformation.getUid().equals(record.getUid())){
                        result.setStatus(false);
                        result.setMsg("您已经参加过该合租了，请勿重复加入");
                    }
                }
            }
        }
        return result;

    }

    @PostMapping("/addselective")
    public void insertSelective(Shareinformation record){
        shareInformationService.insertSelective(record);
    }

    @GetMapping("/getbyhid")
    public Shareinformation selectByHid(String hid){
        return shareInformationService.selectByHid(hid);
    }

    @GetMapping("/getbymid")
    public List<Shareinformation> selectByMid(String mid){
        return shareInformationService.selectByMid(mid);
    }

    @GetMapping("/getbyuid")
    public List<Map> selectByUid(String uid){
        List<Map> infos = new ArrayList<>();
        List<Shareinformation> shareinformationList=shareInformationService.selectByUid(uid);
        for(Shareinformation sh: shareinformationList){
            HashMap<String, Object> info = new HashMap<>();
            Houseinfo houseinfo = houseService.selectByHid(sh.getHid());
            List<String> imgs=new ArrayList<>();
            String img=houseinfo.getHimg();
            String[] is=img.split("!");
            for (String i:is){
                imgs.add(i);
            }
            info.put("house", houseinfo);
            info.put("share", sh);
            info.put("img", imgs.get(0));
            infos.add(info);
        }
//        shareinfor.put("userinformation",shareinformationList);
        return infos;
    }

    @PostMapping("/delete")
    public void deleteByHid(String hid){
        shareInformationService.deleteByHid(hid);
    }

    @PostMapping("/update")
    public void updateByPrimaryKey(Shareinformation record){
        shareInformationService.updateByPrimaryKey(record);
    }

    @PostMapping("/updateselective")
    public void updateByPrimaryKeySelective(Shareinformation record){
        shareInformationService.updateByPrimaryKeySelective(record);
    }
}
